﻿using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace NorthwindDAO
{
    public class ForeignOrders : Entity
    {
        private Int32 _OrderID;

        ///Default constructor
        public ForeignOrders()
        {

        }

        public ForeignOrders(params Entity[] entities)
            : this(true, entities)
        {
        }

        public override string TableName()
        {
            return "Orders";
        }

        /// <summary>
        /// Constructor for joins. 
        /// Available CHILD tables to join with:
        ///    - Order_details ( Order_details_fk_order_details_orders )
        /// Available PARENT tables to join with:
        ///    - Customers
        ///    - Employees
        ///    - Shippers
        /// </summary>
        public ForeignOrders(bool isFlat, params Entity[] entities)
            : this()
        {
            foreach (Entity entity in entities)
            {
                string[] entityParts = entity.GetType().ToString().Split('.');
                string entityName = entityParts[entityParts.Length - 1];
                PropertyInfo pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, new EntityCollection(entity), null);
                }
                entityName = entityName + "_"; ;
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
            }
            SetFlat(isFlat);
        }

        public enum Fields
        {
            Customerid,
            Employeeid,
            Orderdate,
            Requireddate
        }

        [IsNotNullable]
        [IsPK]
        [IsIdentity]
        [SequenceName("Orders")]
        public Int32 Orderid
        {
            get
            {
                return _OrderID;
            }
        }

        [MaxLength(5)]
        public String Customerid { get; set; }

        public Int32 Employeeid { get; set; }

        public DateTime Orderdate { get; set; }

        public DateTime Requireddate { get; set; }

        public DateTime Shippeddate { get; set; }

        public Int32 Shipvia { get; set; }

        [DefaultValue("(0)")]
        public Decimal Freight { get; set; }

        [MaxLength(40)]
        public String Shipname { get; set; }

        [MaxLength(60)]
        public String Shipaddress { get; set; }

        [MaxLength(15)]
        public String Shipcity { get; set; }

        [MaxLength(15)]
        public String Shipregion { get; set; }

        [MaxLength(10)]
        public String Shippostalcode { get; set; }

        [MaxLength(15)]
        public String Shipcountry { get; set; }

        #region Order_details
        private EntityCollection _Order_details;
        [NotInTable]
        [TypeContained(typeof(Order_details), "Order_details")]
        public EntityCollection Order_details
        {
            get
            {
                return _Order_details;
            }

            set
            {
                _Order_details = value;
            }
        }
        private Order_details _Order_details_;
        [NotInTable]
        public Order_details Order_details_
        {
            get
            {
                return _Order_details_;
            }

            set
            {
                _Order_details_ = value;
            }
        }
        #endregion
        #region Customers
        private EntityCollection _Customers;
        [NotInTable]
        [TypeContained(typeof(Customers), "Customers")]
        public EntityCollection Customers
        {
            get
            {
                return _Customers;
            }

            set
            {
                _Customers = value;
            }
        }
        private Customers _Customers_;
        [NotInTable]
        public Customers Customers_
        {
            get
            {
                return _Customers_;
            }

            set
            {
                _Customers_ = value;
            }
        }
        #endregion
        #region Employees
        private EntityCollection _Employees;
        [NotInTable]
        [TypeContained(typeof(Employees), "Employees")]
        public EntityCollection Employees
        {
            get
            {
                return _Employees;
            }

            set
            {
                _Employees = value;
            }
        }
        private Employees _Employees_;
        [NotInTable]
        public Employees Employees_
        {
            get
            {
                return _Employees_;
            }

            set
            {
                _Employees_ = value;
            }
        }
        #endregion
        #region Shippers
        private EntityCollection _Shippers;
        [NotInTable]
        [TypeContained(typeof(Shippers), "Shippers")]
        public EntityCollection Shippers
        {
            get
            {
                return _Shippers;
            }

            set
            {
                _Shippers = value;
            }
        }
        private Shippers _Shippers_;
        [NotInTable]
        public Shippers Shippers_
        {
            get
            {
                return _Shippers_;
            }

            set
            {
                _Shippers_ = value;
            }
        }
        #endregion
        public override DBJoin GetJoin(string tableName)
        {
            if (string.Compare(tableName, "Order_details", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Orderid");
                right.Add(GetType().Name + ".Orderid");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            if (string.Compare(tableName, "Customers", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Customerid");
                right.Add(GetType().Name + ".Customerid");
                return new DBJoin(left, right, JoinTypeEnumeration.INNER);
            }
            if (string.Compare(tableName, "Employees", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Employeeid");
                right.Add(GetType().Name + ".Employeeid");
                return new DBJoin(left, right, JoinTypeEnumeration.INNER);
            }
            if (string.Compare(tableName, "Shippers", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Shipperid");
                right.Add(GetType().Name + ".Shipvia");
                return new DBJoin(left, right, JoinTypeEnumeration.INNER);
            }
            return null;
        }

    }
}